package com.ygqh.baby.service.impl;

import java.util.List;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgProductSecondDetail;
import com.ygqh.baby.mapper.YgProductSecondDetailMapper;
import com.ygqh.baby.service.YgProductSecondDetailService;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgProductSecondDetailExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class YgProductSecondDetailServiceImpl implements YgProductSecondDetailService {

	@Autowired
	private YgProductSecondDetailMapper ygProductSecondDetailMapper;

	@Override
	public List<YgProductSecondDetail> find() {
		YgProductSecondDetailExample example = new YgProductSecondDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductSecondDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgProductSecondDetailExample example = new YgProductSecondDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductSecondDetailMapper.countByExample(example);
	}

	@Override
	public YgProductSecondDetail findById(Long id) {
		return ygProductSecondDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgProductSecondDetail ygProductSecondDetail) {
		return ygProductSecondDetailMapper.insertSelective(ygProductSecondDetail);
	}

	@Override
	public int update(YgProductSecondDetail ygProductSecondDetail) {
		return ygProductSecondDetailMapper.updateByPrimaryKeySelective(ygProductSecondDetail);
	}

	@Override
	public ResultSet<YgProductSecondDetail> search(QueryInfo queryInfo, String q) {
		List<YgProductSecondDetail> list = ygProductSecondDetailMapper.selectSuper(queryInfo, q);
		int count = ygProductSecondDetailMapper.countSuper(q);
		ResultSet<YgProductSecondDetail> resultSet = new ResultSet<YgProductSecondDetail>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgProductSecondDetailExample example = new YgProductSecondDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgProductSecondDetail record = new YgProductSecondDetail();
		record.setStatus(DataStatus.Delete);
		return ygProductSecondDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgProductSecondDetail ygProductSecondDetail) {
		int r = 0;
		if (ygProductSecondDetail.getId() != null) {
			r = this.update(ygProductSecondDetail);
		} else {
			r = this.save(ygProductSecondDetail);
		}
		return r;
	}

	@Override
	public int addBatch(List<YgProductSecondDetail> detailList) {
		int row = 0;
		if (CollectionUtils.isEmpty(detailList)) {
			return row;
		}
		List<YgProductSecondDetail> insertList = new ArrayList<>();
		for (int i = 0; i < detailList.size(); i++) {
			insertList.add(detailList.get(i));
			if (insertList.size() == 30 || i == detailList.size() - 1) {
				row += ygProductSecondDetailMapper.insertBatch(insertList);
				insertList.clear();
			}
		}
		return row;
	}

	@Override
	public int updateBatch(List<YgProductSecondDetail> detailList) {
		int row = 0;
		if (CollectionUtils.isEmpty(detailList)) {
			return row;
		}
		List<YgProductSecondDetail> updateList = new ArrayList<>();
		for (int i = 0; i < detailList.size(); i++) {
			updateList.add(detailList.get(i));
			if (updateList.size() == 30 || i == detailList.size() - 1) {
				row += ygProductSecondDetailMapper.updateBatch(updateList);
				updateList.clear();
			}
		}
		return row;
	}

	@Override
	public List<YgProductSecondDetail> findByProductSecondId(Long productSecondId) {
		if (productSecondId == null) {
			return new ArrayList<>();
		}
		YgProductSecondDetailExample example = new YgProductSecondDetailExample();
		example.createCriteria().andProductSecondIdEqualTo(productSecondId).andStatusEqualTo(DataStatus.Valid.name());
		return ygProductSecondDetailMapper.selectByExample(example);
	}

	@Override
	public List<YgProductSecondDetail> findByProductId(Long productId) {
		if (productId == null) {
			return new ArrayList<>();
		}
		YgProductSecondDetailExample example = new YgProductSecondDetailExample();
		example.createCriteria().andProductIdEqualTo(productId).andStatusEqualTo(DataStatus.Valid.name());
		return ygProductSecondDetailMapper.selectByExample(example);
	}

}
